//
//  PhotoChange.m
//  PhotoClass
//
//  Created by dllo on 15/11/4.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "PhotoChange.h"

@implementation PhotoChange

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */



- (void)dealloc {
    [super dealloc];
}

#pragma mark - 重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        [self creatScrollView];
    }
    return self;
}



- (void)creatScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
       _scrollView.delegate = self;
       _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor redColor];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
    [self addSubview:_scrollView];
    //    内存管理
    [_scrollView release];
}


- (void)setArrayAndDistance:(NSArray *)array withDistance:(NSInteger)distance {
    
    for (int i = 0; i < array.count; i++) {
        //        判断数组里面的元素是不是字符串类型
        if ([array[i] isKindOfClass:[NSString class]]) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width * (i + 1), 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
            imageView.image = [UIImage imageNamed:array[i]];
            [_scrollView addSubview:imageView];
         
            [imageView release];
        }
    }
    
    if ([array[array.count - 1] isKindOfClass:[NSString class]]) {     //  判断是否为字符串
        UIImageView *imageViewLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        imageViewLeft.image = [UIImage imageNamed:array[array.count - 1]];
        [_scrollView addSubview:imageViewLeft];
        [imageViewLeft release];
    }
   
    if ([array[0] isKindOfClass:[NSString class]]) {
        UIImageView *imageViewRight = [[UIImageView alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width * (array.count + 1), 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        imageViewRight.image = [UIImage imageNamed:array[0]];
        [_scrollView addSubview:imageViewRight];
        [imageViewRight release];
    }
   
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * (array.count + 2), 0);
    _count = array.count;
    
   
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _scrollView.frame.size.height - distance, _scrollView.frame.size.width, 20)];
//    _pageControl.pageIndicatorTintColor = [UIColor blackColor];
    _pageControl.numberOfPages = _count;
    
    [_pageControl addTarget:self action:@selector(changePageByPageControl:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_pageControl];
    [_pageControl release];
}

#pragma mark - 轮播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (_scrollView.contentOffset.x == _scrollView.frame.size.width * (_count + 1)) {
        [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];
    } else if (_scrollView.contentOffset.x == 0) {
        [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width * _count, 0)];
    }
 
    _pageControl.currentPage = _scrollView.contentOffset.x / _scrollView.frame.size.width - 1;
}


- (void)changePageByPageControl:(UIPageControl *)page {
    _scrollView.contentOffset = CGPointMake((page.currentPage + 1) * _scrollView.frame.size.width, 0);
}

- (void)setTimer:(NSTimeInterval)timeinterval {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:@selector(carousel) userInfo:nil repeats:YES];
}


- (void)carousel {
    _scrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x + _scrollView.frame.size.width, 0);
    if (_scrollView.contentOffset.x > _scrollView.frame.size.width * _count) {
        [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:NO];
    }
  
    _pageControl.currentPage = _scrollView.contentOffset.x / _scrollView.frame.size.width - 1;
}

@end
